﻿using System.Windows.Data;
using System.Windows.Controls;

namespace Falafel
{
    public class DataFormModeConverter : IValueConverter
    {
        public bool Inverse { get; set; }

        public DataFormModeConverter()
        {
            Inverse = false;
        }

        public object Convert(object value, System.Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return InternalConvert(value, targetType);
        }

        public object ConvertBack(object value, System.Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return InternalConvert(value, targetType);
        }

        private object InternalConvert(object value, System.Type targetType)
        {
            if (value is DataFormMode)
            {
                DataFormMode dataMode = (DataFormMode)value;

                if (targetType.FullName.Contains("DataFormMode"))
                {
                    return dataMode;
                }

                // it wants a boolean here
                if (dataMode == DataFormMode.ReadOnly)
                {
                    return GetBoolean(false);
                }

                return GetBoolean(true);
            }

            return GetBoolean(false);
        }

        private bool GetBoolean(bool original)
        {
            if (Inverse)
            {
                return !original;
            }

            return original;
        }
    }
}